package com.yunji.web.coupon;

import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.coupon.business.CouponBusiness;
import com.yunji.coupon.dto.CouponTmplCreateDTO;
import com.yunji.coupon.dto.CouponTmplPageDTO;
import com.yunji.coupon.vo.CouponTmplListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * @author : peter-zhu
 * @date : 2024/10/22 20:52
 * @description : TODO
 **/
@Api(tags = "优惠券模板")
@RestController
@RequestMapping("/couponTmpl")
public class CouponTmplController {

    @Resource
    private CouponBusiness couponBusiness;

    @ApiOperation("测试接口(前端不用关注)")
    @GetMapping("/test")
    public Response<?> test() {
        return ResponseHelper.success();
    }

    @ApiOperation("创建优惠券模板")
    @PutMapping("/create")
    @PreAuthorize("@ss.hasPermi('coupon:add')")
    public Response<?> create(@Valid @RequestBody CouponTmplCreateDTO dto) {
        return ResponseHelper.handle(couponBusiness.createTmpl(dto), "创建失败")
                .withDataId(dto.getCouponTmplId());
    }

    @ApiOperation("删除优惠券模板")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("@ss.hasPermi('coupon:remove')")
    public Response<?> remove(@PathVariable Long id) {
        return ResponseHelper.handle(couponBusiness.removeCouponTmpl(id), "删除失败");
    }

    @ApiOperation("修改优惠券模板")
    @PostMapping("/modify")
    @PreAuthorize("@ss.hasPermi('coupon:edit')")
    public Response<?> modify(@RequestBody CouponTmplCreateDTO dto) {
        return ResponseHelper.handle(couponBusiness.modifyTmpl(dto), "修改失败");
    }

    @ApiOperation("查询优惠券模板列表")
    @PostMapping("/list")
    public Response<List<CouponTmplListVO>> list(@RequestBody CouponTmplPageDTO dto) {
        return ResponseHelper.successWithPage(couponBusiness.tmplPageList(dto), dto.getTotal());
    }

    @ApiOperation("查询优惠券模板详情")
    @GetMapping("/get/{id}")
    @PreAuthorize("@ss.hasPermi('coupon:detail')")
    public Response<CouponTmplCreateDTO> get(@PathVariable Long id) {
        return ResponseHelper.success(couponBusiness.getCouponTmpl(id));
    }

    @ApiOperation("开始或结束(开始传0 结束传1)")
    @PostMapping("/modifyStatus/{id}/{action}")
    @PreAuthorize("@ss.hasPermi('coupon:start')")
    public Response<?> modifyStatus(@PathVariable Long id, @PathVariable Integer action) {
        return ResponseHelper.handle(couponBusiness.modifyReceiveTime(id, action), "修改失败");
    }

    @ApiOperation("撤回")
    @PostMapping("/modifyRecall/{id}")
    @PreAuthorize("@ss.hasPermi('coupon:back')")
    public Response<?> modifyRecall(@PathVariable Long id) {
        return ResponseHelper.handle(couponBusiness.modifyRecall(id), "撤回失败");
    }

    @ApiOperation("查看名称")
    @GetMapping("/getLikeNames/{name}")
    public Set<String> getLikeNames(@PathVariable String name) {
        return couponBusiness.likeNames(name);
    }

    @ApiOperation("后台优惠券模板列表(导出)")
    @PostMapping("/exportList")
    public void exportList(@RequestBody CouponTmplPageDTO dto, HttpServletResponse response) {
        List<CouponTmplListVO> voList = couponBusiness.tmplList(dto);
        ExcelUtil<CouponTmplListVO> util = new ExcelUtil<>(CouponTmplListVO.class);
        util.exportExcel(response, voList, "优惠券模板列表");
    }
}
